global DATA  "path/to/input/data"   
global OUT "path/to/output/folder"
global INT "path/to/intermediate/files"

local bc_threshold=100	
set scheme tufte

use "$DATA\twincites_applicant_did_prep.dta", clear
drop if backcite_count_citing>`bc_threshold'
sort family_cited priority_citing_d
duplicates drop family_cited nonJP, force
duplicates tag family_cited, gen(tag)
drop if tag==0
drop tag
keep patent_cited US_filing_date_d_cited family_cited
rename family_cited family
save "$INT\filing_dates.dta", replace


set scheme tufte
use "$DATA\US-JP_appdata.dta", clear
keep if appln_auth=="JP"
keep family appln_filing_date
sort family appln_filing_date
duplicates drop family, force
joinby family using "$INT\filing_dates.dta"
drop patent_cited
rename US_filing_date_d_cited US_filing_date
gen JP_filing_date=date(appln_filing_date, "YMD")
gen diff=US_filing_date -JP_filing_date
drop if diff>400
duplicates drop
save "$INT\filing_dates_processed.dta", replace

//=======================================
// FIGURE A3
//=======================================

hist US_filing_date if US_filing_date>14760 & US_filing_date<15126, w(7) xlab(14761 "31/5/2000" 14943 "AIPA" 14976 "1/1/2001" 15065 "31/3/2001" 15125 "30/5/2001") scale(1.5) xti("US filing date", margin(t+2)) yti("Patent count") freq ysize(4) xsize(10) graphregion(margin(r+5))
graph export "$OUT\filing_hist.pdf", as(pdf) name("Graph") replace

hist JP_filing_date if JP_filing_date>14395 & JP_filing_date<15126, w(14) xlab(14611 "1/1/2000" 14700 "31/3/2000" 14943 "AIPA" ) scale(1.5) xti("JP filing date", margin(t+2)) yti("Patent count") freq ysize(4) xsize(10) xline(14761, lpattern(dash) lcolor(red))
graph export "$OUT\filing_hist_JP.pdf", as(pdf) name("Graph") replace

//=======================================
// FIGURE A4
//=======================================

hist diff if US_filing_date<14943, freq xti("Pre-AIPA difference in filing dates (days)") xlab(-370 "-365" 0 "0" 370 "365") ylab(0 300 600) w(30) scale(1.5)  xline(0, lpattern(dash) lcolor(red)) text(400 -150 "US filing first" 400 150 "JP filing first") yti("Patent count") ysize(4) xsize(10)
graph export "$OUT\filing_date_diff_pre.pdf", as(pdf) name("Graph") replace

hist diff if US_filing_date>=14943, freq xti("Post-AIPA difference in filing dates (days)") xlab(-370 "-365" 0 "0" 370 "365") ylab(0 300 600) w(30) scale(1.5)  xline(0, lpattern(dash) lcolor(red)) text(400 -150 "US filing first" 400 150 "JP filing first") yti("Patent count") ysize(4) xsize(10)
graph export "$OUT\filing_date_diff_post.pdf", as(pdf) name("Graph") replace











